package com.hospital.payment.module.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.common.enums.registration.module.RegistrationStatus;
import com.hospital.common.pojo.TRegistration;
import com.hospital.payment.module.exception.TRegistrationServiceException;
import com.hospital.payment.module.exception.enums.TRegistrationServiceExpCode;
import com.hospital.payment.module.mapper.TRegistrationMapper;
import com.hospital.payment.module.service.TRegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class TRegistrationServiceImpl extends ServiceImpl<TRegistrationMapper, TRegistration> implements TRegistrationService {
    @Autowired
    private TRegistrationMapper registrationMapper;
    @Override
    @Transactional
    public void cancelRegistration(Long registrationId) {
        LambdaUpdateWrapper<TRegistration> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(TRegistration::getId, registrationId)
                .set(TRegistration::getState, RegistrationStatus.CANCELED.value);
        if(registrationMapper.update(updateWrapper)==0){
            throw new TRegistrationServiceException(TRegistrationServiceExpCode.CANCEL_REGISTRATION_FAILED);
        }
    }
    @Transactional
    @Override
    public void afterPay(Long registrationId) {
        LambdaUpdateWrapper<TRegistration> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(TRegistration::getId, registrationId)
                .set(TRegistration::getState, RegistrationStatus.COMPLETED.value);
        if ( registrationMapper.update(updateWrapper)==0){
            throw new TRegistrationServiceException(TRegistrationServiceExpCode.AFTER_PAY_FAILED);
        }
    }
}
